package com.wenx.ftpcamel;

import com.alibaba.fastjson.JSON;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by wenx on 2019-07-22 10:07
 */
public class FileTest5 {

    public static void main(String[] args) throws Exception {
        ModelCamelContext camelContext = new DefaultCamelContext();

        camelContext.start();

        camelContext.addRoutes(new RouteBuilder() {

            @Override
            public void configure() {
                LocalDateTime start = LocalDateTime.now();
                System.out.println("开始 "+ start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                from("file:D:\\test?fileName=dmp-server.tar.gz&noop=true")
                        .convertBodyTo(byte[].class)
                        .process(exchange -> {
                            LocalDateTime get = LocalDateTime.now();
                            System.out.println("读取文件完成 "+ get.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                            //System.out.println("exchange = " + exchange.getIn().getBody());
                            Map<String,Object> result = new HashMap<>();
                            result.put("code","0");
                            result.put("data",exchange.getIn().getBody());
                            exchange.getOut().setBody(JSON.toJSONString(result));
                            exchange.getOut().setHeader(Exchange.HTTP_METHOD,"post");
                            exchange.getOut().setHeader(Exchange.CONTENT_TYPE,"application/json");
                        })
                        //.setHeader(Exchange.HTTP_METHOD, constant("POST"))
                        //.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
                        .to("http://localhost:8088/rest/api/fileInputStreamToFile")
                        .convertBodyTo(String.class)
                        .process(exchange -> {
                            System.out.println("result = " + exchange.getIn().getBody());
                            LocalDateTime get = LocalDateTime.now();
                            System.out.println("写入文件完成 "+ get.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        });
            }
        });

        synchronized (FileDemo1.class) {
            FileDemo1.class.wait();
        }
    }

}
